package com.lwj.liwanjia.view.activity.work.shang_jia.ding_dan;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.lwj.liwanjia.R;
import com.lwj.liwanjia.base.BaseActivity;
import com.lwj.liwanjia.bean.DingBean04;
import com.lwj.liwanjia.config.URLConstant;
import com.lwj.liwanjia.utils.SpfUtils;
import com.lwj.liwanjia.view.activity.work.shang_jia.ding_dan.tui_huo_tui_kuan.THTKHomeActivity;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * Created by 001 on 2017/9/21.
 */

public class DingDanHomeActivity extends BaseActivity implements View.OnClickListener {

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            switch (msg.what) {

                case 0:
                    Toast.makeText(DingDanHomeActivity.this, "数据有误", Toast.LENGTH_LONG).show();
                    break;

                case 1:
                    if (DingDanHomeActivity.this != null) {
                        if (!TextUtils.isEmpty(imageUrl)) {
                            Glide.with(DingDanHomeActivity.this).load(imageUrl).into(iv_photo);
                        }

                        if (!TextUtils.isEmpty(jinRi)) {
                            tv_jinRiNumber.setText(jinRi);
                        }

                        if (!TextUtils.isEmpty(tuiHuo)) {
                            tv_tuiKuanNumber.setText(tuiHuo);
                        }

                        if (!TextUtils.isEmpty(daiFuKuan)) {
                            tv_daiFuKuanNumber.setText(daiFuKuan);
                        }

                        if (!TextUtils.isEmpty(daiFaHuo)) {
                            tv_daiFaHuoNumber.setText(daiFaHuo);
                        }

                        if (!TextUtils.isEmpty(yiFaHuo)) {
                            tv_yiFaHuoNumber.setText(yiFaHuo);
                        }

                        if (!TextUtils.isEmpty(yiWanCheng)) {
                            tv_yiWanCheng.setText(yiWanCheng);
                        }
                    }
                    break;
            }
        }
    };

    private TextView tv_back;
    private TextView tv_title;
    private LinearLayout ll_jinRiDingDan;//今日订单
    private LinearLayout ll_tuiHuoTuiKuan;//退货退款
    private LinearLayout ll_daiFuKuan;//待付款
    private LinearLayout ll_daiFaHuo;//待发货
    private LinearLayout ll_yiFaHuo;//已发货
    private LinearLayout ll_yiWanCheng;//已完成
    private TextView tv_jinRiNumber;//今日订单
    private TextView tv_tuiKuanNumber;//退货退款
    private TextView tv_daiFuKuanNumber;//待付款
    private TextView tv_daiFaHuoNumber;//待发货
    private TextView tv_yiFaHuoNumber;//已发货
    private TextView tv_yiWanCheng;//已完成
    private String imageUrl;
    private String jinRi;
    private String tuiHuo;
    private String daiFuKuan;
    private String daiFaHuo;
    private String yiFaHuo;
    private String yiWanCheng;
    private ImageView iv_photo;

    @Override
    protected void initView() {

        String shopname = getIntent().getStringExtra("shopname");
        tv_back = (TextView) findViewById(R.id.tv_left);
        tv_title = (TextView) findViewById(R.id.tv_title);

        ll_jinRiDingDan = (LinearLayout) findViewById(R.id.ll_jinRiDingDan);
        ll_tuiHuoTuiKuan = (LinearLayout) findViewById(R.id.ll_tuiHuoTuiKuan);
        ll_daiFuKuan = (LinearLayout) findViewById(R.id.ll_daiFuKuan);
        ll_daiFaHuo = (LinearLayout) findViewById(R.id.ll_daiFaHuo);
        ll_yiFaHuo = (LinearLayout) findViewById(R.id.ll_yiFaHuo);
        ll_yiWanCheng = (LinearLayout) findViewById(R.id.ll_yiWanCheng);

        tv_jinRiNumber = (TextView) findViewById(R.id.tv_jinRiNumber);
        tv_tuiKuanNumber = (TextView) findViewById(R.id.tv_tuiKuanNumber);
        tv_daiFuKuanNumber = (TextView) findViewById(R.id.tv_daiFuKuanNumber);
        tv_daiFaHuoNumber = (TextView) findViewById(R.id.tv_daiFaHuoNumber);
        tv_yiFaHuoNumber = (TextView) findViewById(R.id.tv_yiFaHuoNumber);
        tv_yiWanCheng = (TextView) findViewById(R.id.tv_yiWanCheng);
        iv_photo = (ImageView) findViewById(R.id.iv_photo);

        tv_back.setVisibility(View.VISIBLE);
        tv_title.setVisibility(View.VISIBLE);
        tv_title.setText(shopname + "");

        tv_back.setOnClickListener(this);
        ll_jinRiDingDan.setOnClickListener(this);
        ll_tuiHuoTuiKuan.setOnClickListener(this);
        ll_daiFuKuan.setOnClickListener(this);
        ll_daiFaHuo.setOnClickListener(this);
        ll_yiFaHuo.setOnClickListener(this);
        ll_yiWanCheng.setOnClickListener(this);

    }

    @Override
    protected void initData() {

        //连接网络 获取数据
        initOkHttp();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_ding_dan_home;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //返回
            case R.id.tv_left:
                finish();
                break;
            //今日订单
            case R.id.ll_jinRiDingDan:
                startActivity(new Intent(this, JinRiDingDanHomeActivity.class));
                break;
            //退货退款
            case R.id.ll_tuiHuoTuiKuan:
                startActivity(new Intent(this, THTKHomeActivity.class));
                break;
            //待付款
            case R.id.ll_daiFuKuan:
                startActivity(new Intent(this, DaiFuKuanHomeActivity.class));
                break;
            //待发货
            case R.id.ll_daiFaHuo:
                startActivity(new Intent(this, DaiFaHuoHomeActivity.class));
                break;
            //已发货
            case R.id.ll_yiFaHuo:
                startActivity(new Intent(this, YiFaHuoHomeActivity.class));
                break;
            //已完成
            case R.id.ll_yiWanCheng:
                startActivity(new Intent(this, YiWanChengHomeActivity.class));
                break;
        }
    }

    //连接网络 获取数据
    private void initOkHttp() {

        String url = URLConstant.BASE_URL + "order/Shop/baseOrder";
        String token = SpfUtils.getSpfUtils(this).getToken();

        OkHttpClient mOkHttpClient = new OkHttpClient();
        RequestBody formBody = new FormBody.Builder()
                .add("token", token)
                .build();
        Request request = new Request.Builder()
                .url(url)
                .post(formBody)
                .build();
        Call mCall = mOkHttpClient.newCall(request);
        mCall.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

                try {
                    String result = response.body().string();
                    parse01(result);
                } catch (Exception e) {

                }

            }
        });
    }

    //解析
    private void parse01(String result) {

        Gson gson = new Gson();
        DingBean04 bean = gson.fromJson(result, DingBean04.class);

        if (bean != null) {

            switch (bean.getState()) {

                case 0:
                    mHandler.sendEmptyMessage(0);
                    break;

                case 1:

                    imageUrl = bean.getData().getTopPic();
                    jinRi = bean.getData().getTodayOrder() + "";
                    tuiHuo = bean.getData().getReturnX() + "";
                    daiFuKuan = bean.getData().getNoPay() + "";
                    daiFaHuo = bean.getData().getNoSend() + "";
                    yiFaHuo = bean.getData().getSend() + "";
                    yiWanCheng = bean.getData().getEnd() + "";

                    mHandler.sendEmptyMessage(1);

                    break;
            }
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        initOkHttp();
    }
}
